<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
global $data, $admin;

/**
 * Description of empresas
 *
 * @author Golem
 */
class Material extends CI_Controller {

    public function __construct() {
        parent::__construct();
        global $admin, $data;
        # cargamos variables
        $admin = $this->session->userdata('admin');
        $admin['place'] = 'material';
        # verifica si pasó el home
        if ($admin['ta_tipo_administrador_id'] != 1) {
            redirect('home');
        }
        $this->output->enable_profiler(FALSE);
    }

    public function index() {
        global $data, $admin;

        $data['material'] = $this->cc_model_a->lista_material();

        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_admin', $data);
        $this->load->view('material/content');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function alta() {
        global $data, $admin;

        $data['empresa'] = $this->cc_model_a->cEmpresas();
        $data['division'] = $this->cc_model_a->lista_divisiones();
        $data['tipomaterial'] = $this->cc_model_a->lista_tipo_materiales();
        $data['mensaje'] = $this->session->userdata('mensaje');

        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_admin', $data);
        $this->load->view('material/alta');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
        
        $this->session->unset_userdata('mensaje');
    }

    public function save() {
        $this->form_validation->set_rules('sm_sub_modulo_id', 'sm_sub_modulo_id', 'required|trim|xss_clean|max_length[5]');
        $this->form_validation->set_rules('m_nombre', 'm_nombre', 'required|trim|xss_clean|max_length[65]');
        $this->form_validation->set_rules('m_descripcion', 'm_descripcion', 'required|trim|xss_clean|max_length[65000]');
        $this->form_validation->set_rules('m_minimo', 'm_minimo', 'required|trim|xss_clean|max_length[4]');
        $this->form_validation->set_rules('m_orden', 'm_orden', 'required|trim|xss_clean|max_length[5]');
        $this->form_validation->set_rules('tm_tipo_material_id', 'tm_tipo_material_id', 'required|trim|xss_clean|max_length[5]');


        # has the form been submitted and with valid form info (not empty values)
        if (!$this->form_validation->run()) {
            $errorMsj = '   <div class="alert alert-error" style="margin-top: 16px;">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>Error al validar el formulario </strong> </div>';
            $this->session->set_userdata('mensaje', $errorMsj);
            redirect('material/alta');
        }
        $sm_sub_modulo_id = $this->security->xss_clean($this->input->post('sm_sub_modulo_id'));
        $m_nombre = $this->security->xss_clean($this->input->post('m_nombre'));
        $m_descripcion = $this->security->xss_clean($this->input->post('m_descripcion'));
        $m_minimo = $this->security->xss_clean($this->input->post('m_minimo'));
        $m_orden = $this->security->xss_clean($this->input->post('m_orden'));
        $tm_tipo_material_id = $this->security->xss_clean($this->input->post('tm_tipo_material_id'));
        $m_examen = true;
        
        #configuraciones de carga
        $config['upload_path'] = './assets/docs/';
        $config['allowed_types'] = 'mp4|mp3|pdf|swf';
        $config['max_size'] = '100000';
        $config['overwrite'] = FALSE;
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = TRUE;

        #carga
        $this->load->library('upload', $config);
        #si la carga falla regresamos
        if (!$this->upload->do_upload('myfile')) {
            $errorMsj = '   <div class="alert alert-error" style="margin-top: 16px;">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>Error al cargar el documento </strong> ' . $this->upload->display_errors() . '</div>';
            $this->session->set_userdata('mensaje', $errorMsj);
            redirect(base_url('material/alta'));
        }
        #carga de documento correcta, recuperamos información
        $carga = $this->upload->data();
        $m_nombre_en_disco = $this->randomString().$carga['file_ext'];
        $m_nombre_documento = $carga['client_name'];
        
        /**
         * Recordatorio
         *  MODIFICAR LA VISTA PARA QUE RECIBA LA OPCION DE SI VA A TENER 
         * EXAMEN DICHO MATERIAL
         */
        $this->cc_model_a->gMaterial($sm_sub_modulo_id, $m_nombre, $m_nombre_documento, $m_nombre_en_disco, $m_orden, $m_descripcion, $m_minimo, $tm_tipo_material_id,$m_examen);
        redirect('material');
    }

    public function randomString(){  
        $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $string = '';
        for ($i = 0; $i < 10; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $string;
    }

    public function jsonlist() {
        $sm_sub_modulo_id = $this->input->post('name');
        $materiales = $this->cc_model_a->listaMateriales_xSModulo($sm_sub_modulo_id);
        $this->output->set_output(json_encode($materiales));
    }

}
